package com.uit3j.cloud.search.listener;

import com.uit3j.cloud.api.product.bo.EsHotSearchBO;
import com.uit3j.cloud.api.product.feign.HotSearchFeignClient;
import com.uit3j.cloud.common.exception.Uit3jcloudException;
import com.uit3j.cloud.common.rocketmq.config.RocketMqConstant;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 热搜的监听
 * @author tan
 * @date 2024/07/10
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.HOT_SEARCH_DELETE,consumerGroup = RocketMqConstant.HOT_SEARCH_DELETE)
public class HotSearchDeleteConsumer implements RocketMQListener<EsHotSearchBO> {

    private static final Logger log = LoggerFactory.getLogger(HotSearchDeleteConsumer.class);

    @Autowired
    private HotSearchFeignClient hotSearchFeignClient;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 插入热搜时，此时插入es
     */
    @Override
    public void onMessage(EsHotSearchBO sendHotSearch) {
        Long hotSearchId = sendHotSearch.getHotSearchId();
        DeleteRequest request = new DeleteRequest("hot_search",String.valueOf(hotSearchId));
        try {
            DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
            log.info("删除热搜保存es回调...: " + deleteResponse.toString());
        } catch (IOException e) {
            log.error(e.toString());
            throw new Uit3jcloudException("更新订单es信息异常",e);
        }
    }


}
